<template>
	<view class="content" style="height: 100vh;">
		<search @tap="navigateToSearch()" disabled="true"></search>
		<view>
			<scroll-view class="ca_left" scroll-y>
				<view v-for="(item,index) in category" :key="index" class="ca_item" @tap="choiceCategory(item.id)">
					<view v-if="active==item.id" class="vertical"></view>
					<view :class="active==item.id?'ca_name_':'ca_name'">{{item.name}}</view>
				</view>
			</scroll-view>
			<scroll-view class="ca_right" scroll-y @scrolltolower="loadProduct()">
				<view class="ca_right_view">
					<view v-for="item in productList" :key="item.id" class="item" @tap="goProductDetail(item.id)">
						<image class="pro_img" :src="item.image"></image>
						<view class="pro_name_view">
							<view class="pro_name">{{item.name}}</view>
							<view class="pro_salenum">销量 {{item.saleNum}}</view>
							<view style="justify-content: space-between;margin-top: 5upx;">
								<view style="align-items: flex-end;color: #FE0036;font-size: 24upx;">
									￥
									<view style="font-size: 32upx;font-weight: 700;">{{item.price}}</view>
								</view>
								<image style="width: 35upx;height: 35upx;" src="../../static/icon/redc.png"></image>
							</view>

						</view>
					</view>
				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import api from '../../common/api.js';
	import search from '../../components/search.vue';
	import {
		get_,
		getWithParam_
	} from '../../common/request.js';
	export default {
		components: {
			search
		},
		data() {
			return {
				active: 0,
				category: [],
				productList: [],
				loadMore: true,
				page: 1,
				pageSize: 10

			}
		},
		onLoad(option) {
			if(option.categoryId!=null){
				this.active=option.categoryId
			}
			this.initData()
		},
		methods: {
			loadProduct(){
				this.page=this.page+1
				if(this.loadMore){
					this.getProduct()
				}
			},
			navigateToSearch() {
				uni.navigateTo({
					url: "../product/searchProduct"
				})
			},
			goProductDetail(id) {
				
				uni.navigateTo({
					url: "../product/detail?id="+id
				})
			},
			choiceCategory(id) {
				this.loadMore =true
				this.active = id
				this.page = 1
				this.productList = []
				this.getProduct()
			},
			getProduct() {
				let param = {
					page: this.page,
					pageSize: this.pageSize,
					categoryId: this.active
				}
				getWithParam_(api.SEARCH, param).then(res => {
					if (res.data == null || res.data.length < 10) {
						this.loadMore = false
					}
					this.productList = this.productList.concat(res.data)
				})
			},
			initData() {
				get_(api.CATEGORY).then(res => {
					this.category = res.data
					if(this.active==0){
						this.active = res.data[0].id
					}
					this.getProduct()
				})
			}
		}
	}
</script>

<style>
	.ca_right_item_name {
		font-size: 24upx;
		color: #888888;
		font-weight: 500;
		padding: 20upx 0;
	}

	.ca_right_item_img {
		width: 100upx;
		height: 100upx;

	}

	.ca_right_item {
		width: 33.3%;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin: 20upx 0;
	}

	.ca_right_view {
		flex-direction: column;
		align-items: center;
	}

	.ca_right_img {
		width: 500upx;
		height: 200upx;
	}

	.ca_name {
		justify-content: center;
		width: 100%;
		font-size: 30upx;
		color: #555555;
	}

	.ca_name_ {
		justify-content: center;
		width: 100%;
		font-size: 35upx;
		color: #FF0000;
		font-weight: 600;
	}

	.ca_item {
		width: 100%;
		position: relative;
		height: 80upx;
		align-items: center;
	}

	.vertical {
		position: absolute;
		left: 0;
		width: 4px;
		height: 40upx;
		background-color: #FE0338;
	}

	.ca_left {
		width: 200upx;
		height: calc(100vh - 100upx);
		border-right: 1px solid #F2F3F5;
		background-color: #FFFFFF;
		flex-direction: column;
	}

	.ca_right {
		width: 550upx;
		height: calc(100vh - 100upx);
		background-color: #FFFFFF;
	}

	.item {
		margin: 10upx 0;
		width: 500upx;
		border-radius: 10upx;
		padding: 10upx;
		box-sizing: border-box;
		align-items: center;
	}

	.pro_img {
		width: 150upx;
		height: 150upx;
	}

	.pro_name_view {
		flex-direction: column;
		flex: 1;
		margin-left: 20upx;
	}

	.pro_name {
		color: #333333;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.pro_salenum {
		color: #999999;
		font-size: 24upx;
		margin-top: 15upx;
	}
</style>
